//
//  UIImageView + WebImage.swift
//  SwiftWeibo
//
//  Created by 刘英群 on 2017/2/9.
//  Copyright © 2017年 刘英群. All rights reserved.
//

import SDWebImage

extension UIImageView{
    
    func cz_setImage(urlString: String?, placeholderImage: UIImage?, isAvatar: Bool = false) {
        
        //处理url
        guard let urlString = urlString,
            let url = URL(string: urlString) else {
        
            //设置占位图像
            image = placeholderImage
            
            return
        }
        
        //可选项只能用在swift， oc有的使用用 ! 同样可以传入nil
        sd_setImage(with: url,
                    placeholderImage: placeholderImage,
                    options: [],
                    progress: nil) {[weak self] (image, _, _, _) in
               
                    //完成回调 - 判断是否是头像
                        if isAvatar {
                            
                            self?.image = image?.cz_avatarImage(size: self?.bounds.size)
                        }
        }
    }
}
